package com.tianma315.admin.qa.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.qa.domain.QaPatrolDO;
import com.tianma315.admin.qa.service.QaCheckService;
import com.tianma315.admin.qa.vo.QaPatrolVO;
import com.tianma315.admin.qa.vo.QaSpotCheckVO;
import com.tianma315.admin.sys.domain.UserDO;
import com.tianma315.admin.sys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.qa.dao.QaSpotCheckDao;
import com.tianma315.admin.qa.domain.QaSpotCheckDO;
import com.tianma315.admin.qa.service.QaSpotCheckService;

import javax.xml.ws.Action;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 品控抽查
 * </pre>
 * <small> 2019-07-09 16:02:56 | wen</small>
 */
@Service
public class QaSpotCheckServiceImpl extends CoreServiceImpl<QaSpotCheckDao, QaSpotCheckDO> implements QaSpotCheckService {


    @Autowired
    private QaSpotCheckDao qaSpotCheckDao;

    @Autowired
    private UserService userService;

    @Override
    public Page<QaSpotCheckVO> getQaSpotCheckPage(Integer pageNumber, Integer pageSize, QaSpotCheckDO qaSpotCheckDTO, Date beginDate,
                                                  Date endDate) {
        // 查询列表数据
        Page<QaSpotCheckVO> page = new Page<>(pageNumber, pageSize);
        List<QaSpotCheckVO> list = qaSpotCheckDao.getSpotCheckVOPage(page,qaSpotCheckDTO,beginDate,endDate);
        page.setRecords(list);
        return page;
    }

    @Override
    public Boolean addQaSpotCheck(QaSpotCheckDO qaSpotCheckDO) {
        qaSpotCheckDO.setState(0);
        qaSpotCheckDO.setCreateDate(Calendar.getInstance().getTime());
        Boolean result = insert(qaSpotCheckDO);
        return result;
    }

    @Override
    public Boolean deleteQaSpotCheck(Integer id) {
        QaSpotCheckDO qaSpotCheckDO = new QaSpotCheckDO();
        qaSpotCheckDO.setState(1);
        qaSpotCheckDO.setQaSpotCheckId(id);
        Boolean result = updateById(qaSpotCheckDO);
        return result;
    }

    @Override
    public QaSpotCheckVO getQaSpotCheckByProductIdAndBatch(Long productId, String batch) {
        Wrapper<QaSpotCheckDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",productId);
        wrapper.eq("batch",batch);
        wrapper.eq("state",0);
        QaSpotCheckDO qaSpotCheckDO = selectOne(wrapper);
        QaSpotCheckVO qaSpotCheckVO = new QaSpotCheckVO();
        if (qaSpotCheckDO != null) {
            UserDO user = userService.selectById(qaSpotCheckDO.getCheckBy());
            BeanUtils.copyProperties(qaSpotCheckDO,qaSpotCheckVO);
            qaSpotCheckVO.setCheckName(user.getName());
        }
        return qaSpotCheckVO;
    }
}
